//
//  UserInfoViewController.m
//  PCR
//
//  Created by shumei on 16/7/7.
//  Copyright © 2016年 shumei. All rights reserved.
//

#import "UserInfoViewController.h"
#import "AppDelegate.h"

@interface UserInfoViewController (){
    NSArray * _nameArray;
    //相册，相机数组
    NSArray * ALarray;
    UITableView * table_view;
}
@end

@implementation UserInfoViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTopTitleWithTitle:@"个人信息"];
    [self setLeftItemWithTitle:nil OrImage:@"navLeftBtn.png"];
    _nameArray = [[NSArray alloc]initWithObjects:@"地区",@"性别", nil];
    ALarray = [[NSArray alloc]initWithObjects:@"拍照",@"相册", nil];
    [self creatTableView];
    [[YMBaseTabBarViewController shareInstance] setTabbarHidden];
}

-(void)creatTableView
{
    table_view = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,MainSCreen.width , MainSCreen.height - nav_Height) style:UITableViewStylePlain];
    table_view.delegate      = self;
    table_view.dataSource    = self;
    table_view.rowHeight     = 100;
    table_view.backgroundColor = TableView_Default_BGColor;
    [table_view setTableFooterView:[[UIView alloc] init]];
    [self.view addSubview:table_view];
}
#pragma mark ---------------------------------UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    return 2;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 0.1f;
    }
    return TableView_Default_SectionHight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 60 / 667.0 * MainSCreen.height;
    }
    return TableView_Default_RowHight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr1 = @"cellstr";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr1];
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellStr1];
        
        UILabel * name                = [[UILabel alloc]init];
        name.font                     = [UIFont systemFontOfSize:Default_FontSize];
        name.tag                      = 100;
        [cell addSubview:name];
        name.sd_layout.leftSpaceToView(cell,15).widthIs(80).heightRatioToView(cell,1);
        
        UIImageView * imageView = [[UIImageView alloc]init];
        imageView.tag           = 200;
        [cell addSubview:imageView];
        imageView.sd_layout.rightSpaceToView(cell,10).centerYEqualToView(cell).heightRatioToView(cell,0.75).widthEqualToHeight();
        [imageView updateLayout];
        
       
        if (indexPath.section != 0)
        {
            
            UILabel * detail              = [[UILabel alloc]init];
            detail.font                   = [UIFont systemFontOfSize:Default_FontSize];
            detail.tag                    = 300;
            detail.textColor              = [UIColor lightGrayColor];
            [cell addSubview:detail];
            detail.sd_layout.rightSpaceToView(cell,-5 - imageView.frame.size.width).heightRatioToView(cell,1).widthIs(80);
            
            
        }

        
    }
    
    UILabel * name          = (UILabel *)[cell viewWithTag:100];
    name.text               = _nameArray[indexPath.row];
    if (indexPath.section == 0)
    {
        name.text = @"头像";

    }

    UIImageView * header_imageView = (UIImageView *)[cell viewWithTag:200];
    header_imageView.contentMode   = UIViewContentModeScaleAspectFit;
    header_imageView.clipsToBounds = YES;
    header_imageView.image         = [UIImage imageNamed:@"headImg.png"];
    if (indexPath.section != 0)
    {
        header_imageView.hidden = YES;
    }
    
    UILabel * detail        = (UILabel *)[cell viewWithTag:300];
    detail.text             = @"上海";
    
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0)
    {
        //更换头像
        [self changeImage];
    }
    
}

#pragma mark --------------------------------------- 更换头像
-(void)changeImage
{
    ALActionSheetView * actionSheetView = [ALActionSheetView showActionSheetWithTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:ALarray handler:^(ALActionSheetView *actionSheetView, NSInteger buttonIndex)
                                          {
                                              
                                              if (buttonIndex != -1)
                                              {
                                                  //                y = 1;
                                                  if (buttonIndex == 0)//拍照
                                                  {
                                                      [self takePhoto];
                                                  }
                                                  else//相册
                                                  {
                                                      [[UIApplication sharedApplication]setStatusBarStyle:0];
                                                      [self creatPickerControll:UIImagePickerControllerSourceTypePhotoLibrary];
                                                  }
                                                  
                                              }
                                          }];
    [actionSheetView show];
    
    
}

-(void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        
        [self creatPickerControll:sourceType];
        
    }
    else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
  
}
//创建图片选择器
-(void)creatPickerControll:(UIImagePickerControllerSourceType)sourceType
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    //设置拍照后的图片可被编辑
    picker.allowsEditing = YES;
    picker.sourceType = sourceType;
    AppDelegate * app = [UIApplication sharedApplication].delegate;
    [app presentViewController:picker];
}
#pragma mark ----------------------------UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    //获取到选择好的图片
    UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    
    
    UITableViewCell * cell = [table_view cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    UIImageView * header_imageView = [cell viewWithTag:200];
    //显示这张图片
    header_imageView.image = image;

    
//    UIImage *editedImage = [[UIImage alloc] init];
//    
//    editedImage = image;
//    
//    CGRect rect = CGRectMake(0, 0, 100, 100);
//    
//    UIGraphicsBeginImageContext(rect.size);
//    
//    [editedImage drawInRect:rect];
//    
//    editedImage = UIGraphicsGetImageFromCurrentImageContext();
//    
//    NSData *data;
//    
//    if (UIImagePNGRepresentation(editedImage) == nil) {
//        
//        data = UIImageJPEGRepresentation(editedImage, 1);
//        
//    } else {
//        
//        data = UIImagePNGRepresentation(editedImage);
//        
//    }
//    NSData *image_data = UIImagePNGRepresentation(image);
    
    //退出图片选择界面
    [[UIApplication sharedApplication] setStatusBarStyle:1];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    
    
    
}
//当点击图片选择界面取消按钮的时候
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [[UIApplication sharedApplication] setStatusBarStyle:1];
    [picker dismissViewControllerAnimated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
